<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_form_Form
{
	private static $instance;

	/**
	 * @var f_core_Request
	 */
	protected $request;
	protected $moduleName;
	protected $documentName;
	protected $formName;
	protected $params;
	protected $lang;

	/**
	 * @return f_form_Form
	 */
	protected function __construct()
	{
		$this->lang = f_core_Context::getInstance()->getLang();
		$this->request = f_core_Request::getInstance();
		return $this;
	}

	/**
	 * Get singleton instance
	 *
	 * @return f_form_Form
	 */
	public static function getInstance()
	{
		if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}


	/**
	 * Check if form is submitted
	 *
	 * @return boolean
	 */
	public function isSubmitted()
	{
		if(isset($this->params['formValues']) && count($this->params['formValues']) > 0)
		{
			return true;
		}
		
		return false;
	}

	/**
	 * Return true if submitted form has errors
	 *
	 * @return boolean
	 */
	public function hasErrors()
	{
		return sizeof($this->params['errors']);
	}

	/**
	 * Returns error array
	 *
	 * @return array errors
	 */
	public function getErrors()
	{
		return $this->params['errors'];
	}

	/**
	 * @return string
	 */
	public function getFormName()
	{
		return $this->formName; 
	}
	
	/**
	 * @param string $formName
	 * @return f_form_Form
	 */
	public function setFormName($formName)
	{
		$this->formName = $formName;
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function getModuleName()
	{
		return $this->moduleName;
	}
	
	/**
	 * @param string $moduleName
	 * @return f_form_Form
	 */
	public function setModuleName($moduleName)
	{
		$this->moduleName = $moduleName;
		return $this;
	}

	/**
	 * @return string
	 */
	public function getDocumentName()
	{
		return $this->documentName;
	}	
	
	/**
	 * @param string $documentName
	 * @return f_form_Form
	 */
	public function setDocumentName($documentName)
	{
		$this->documentName = $documentName;
		return $this;
	}	
		
	/**
	 * Get form parameters
	 * @return array
	 */
	public function getParams()
	{
		$params = array();
		$params['formName'] = $this->formName;
		$params['formValues'] = $this->request->getParameterValues($this->formName);

		// In case of files upload
		if(isset($_FILES))
		{
			// For each entries of the global $_FILES
			foreach($_FILES as $keyField => $nameField)
			{
				// For each Parameters of the current entry (name, type, tmp_name , etc...)
				foreach($nameField as $paramName => $paramValue)
				{
					// If current parameters is an array 
					if(is_array($paramValue))
					{
						// Mean that format name field in the form is : "name[ 'subname'] " , so
						
						// For each subname
						foreach($paramValue as $realNameField => $value)
						{
							// Add  a subname array ( paramName => value ) in formValues 
							$params['formValues'][$realNameField][$paramName] = $value;
						}
					}
					else
					{
						// Add  a nameField array ( paramName => paramValue ) in formValues 
						$params['formValues'][$keyField][$paramName] = $paramValue;
					}
				}
			}
		}
		
		$params['errors'] = "";
		$this->params = $params;
		return $this->params;
	}
	
	/**
	 * @param array $params
	 * @return f_form_Form
	 */
	public function setParams($params)
	{
		$this->params = $params;
		return $this;
	}		
}